package co.edu.uniquindio.sicorrespondencia.ejb;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

import co.edu.uniquindio.sicorrespondencia.entities.UsuarioRol;
import co.edu.uniquindio.sicorrespondencia.interfaz.IUsuarioRolLocal;

@Name("authenticator")
@Scope(ScopeType.SESSION)
public class Authenticator
{
    @Logger private Log log;

    @In Identity identity;
    @In Credentials credentials;
    
    @In (create=true)
    IUsuarioRolLocal usuarioBean;
    
    
    private UsuarioRol usuariorol;
    
    public boolean authenticate()
    {
        log.info("authenticating {0}", credentials.getUsername());
        //write your authentication logic here,
        //return true if the authentication was
        //successful, false otherwise
        usuariorol = usuarioBean.login(credentials.getUsername(), credentials.getPassword());
        
        if (usuariorol != null)
        {
            identity.addRole(usuariorol.getRol().getRol());
            return true;
        }
        return false;
    }

}
